<?
require_once("defines.inc");

class Banco {
	private $connection;
  private $query;

  private $hostName;
  private $userName;
  private $password;
  private $database;
  private $portNumb;

	// construtor
  public function __construct($mysqlHost,$myslUser,$mysqlPass,$mysqlBase,$mysqlPort = 3306) {
    $this->hostName = $mysqlHost;
    $this->userName = $myslUser;
    $this->password = $mysqlPass;
    $this->database = $mysqlBase;
    $this->portNumb = $mysqlPort;
    $this->connection = null;
    $this->query = "";
  }

  // destrutor
  public function __destruct(){
    $this->desconectar();
  }

  // Inicia a Conexão com o banco de dados
  public function conectar($autoCommit = true) {
    if (!isset($this->connection))
  	  $this->connection = new mysqli($this->hostName, $this->userName, $this->password, $this->database, $this->portNumb);

    if ($this->connection) {
			$this->connection->autocommit($autoCommit);
      return true;
    } else {
    	return false;
    }
	}

  // Fecha Conexão com a Base de Dados
  public function desconectar() {
    if (isset($this->connection)) {
      $this->connection->close();
      $this->connection = null;
    }
  }

	// Função Anti SQL Injection
  private function antiSQLinjection($recebe){
  	$recebe = get_magic_quotes_gpc() == 0 ? addslashes($recebe) : $recebe; // Verifica a configuração de "magic_quotes_gpc"
    $recebe = strip_tags($recebe); // Limpa as tags HTML
    $recebe = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|\\\\)/"),"",$recebe);
    $recebe = preg_replace("@(--|\#|\*|;|=)@s", "", $recebe);
    return $recebe;
  }

  // INSERT
  public function inserir($tabela, $dados) {
		// Dividimos o Array dentro de $dados em Chaves e Valores
		$campos = array_keys($dados);
		$valores = array_values($dados);

		$this->conectar();
		$this->connection->query("INSERT INTO " . $tabela . " (" . implode(', ', $campos) . ") VALUES ('" . implode('\', \'', $valores) . "')");

		if($this->query) {
			return true;
		} else {
			return false;
		}
		$this->desconectar();
	}

	private function MontarWhere($CamposWhere) {
		$i = 0;
		$Fim = count($CamposWhere);
		$ClausulaWhere = "";

		foreach($CamposWhere as $Campo => $Valor) {
			$i++;

			if($i != $Fim) {
				$ClausulaWhere .= $Campo . " = '" . $Valor . "' and ";
			} else {
				$ClausulaWhere .= $Campo . " = '" . $Valor . "'";
			}
		}
		return $ClausulaWhere;
	}

  // DELETE
  public function deletar($Tabela, $CamposWhere) {
		$Where = MontarWhere($CamposWhere);

		$this->conectar();
		$this->connection->query("DELETE FROM " . $Tabela . " WHERE " . $Where);

		if($this->query) {
    	return true;
    } else {
      return false;
    }
		$this->desconectar();
  }

  private function MontarSet($Dados) {
		$i = 0;
		$fim = count($Dados);
		$ClausulaSet = "";

		foreach($Dados as $Campo => $Valor) {
			$i++;

			if ($i != $fim) {
				$ClausulaSet .= $Campo . " = '" . $Valor . "',";
			} else {
				$ClausulaSet .= $Campo . " = '" . $Valor . "'";
			}
		}
		return $ClausulaSet;
  }

  // UPDATE
  public function atualizar($Tabela, $Dados, $CamposWhere) {

		$ClausulaSet = MontarSet($Dados);
		$ClausulaWhere = MontarWhere($CamposWhere);

		$this->conectar();
		$this->connection->query("UPDATE " . $Tabela . " SET " . $ClausulaSet . " WHERE " . $ClausulaWhere);

		if ($this->query) {
    	return true;
    } else {
    	return false;
    }
		$this->desconectar();
  }

	// Selecionar
	public function selecionar($Tabelas, $Campos, $CamposWhere) {
			$ClausulaWhere = $this->MontarWhere($CamposWhere);

			$this->conectar();
      $sql = "SELECT " . implode(", ", $Campos) . " FROM " . $Tabelas . " WHERE " . $ClausulaWhere;

      if (_EMDESENVOLVIMENTO === true) {
        $ResultSet = $this->connection->query($sql) or die("Erro ao executar \"mysqli::query\"<br/>\n" . "Erro: " . $this->connection->error . "<br/>\nString SQL: " . $sql);
      } else {
        $ResultSet = $this->connection->query($sql);
      }

//			$this->desconectar();
			return $ResultSet;
	}
}
?>